<template>
  <BaseModal :title="modalTitle" :visible="visible" @cancel="handleCancel" @ok="handleOk" width="800px" destroyOnClose>
    <custom-form
      ref="ruleForm"
      :isValidate="true"
      :editModel="''"
      :rules="rules"
      :form="formInfo"
      :formList="formItemList"
      @selectNode="handleSelectOrg"
    ></custom-form>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig" :isCloseUpper="false" @close="closeSelectApprover"></selectApprover>
  </BaseModal>
</template>

<script>
import BaseModal from '@/components/BaseModal'
import PartyTreeSelecgt from '../componments/partyTreeSelect.vue'
import { packageConfirm } from '@/utils/baseAction.js'
import { inwardTransfer, transferOut } from '@/api/partyMasses'

export default {
  name:'a'+ Date.now(),
  components: {
    BaseModal,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    modalType: {
      type: String,
      default: 'turnIn'
    },
    initData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      formInfo: {},
      rules: {
        partyOrgId: [{ required: true, message: '请选择', trigger: 'change' }],
      },
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null
    }
  },
  computed: {
    modalTitle() {
      const map = {
        turnIn: '对内转出',
        turnOut: '对外转出'
      }
      return map[this.modalType]
    },
    formItemList() {
      return this.modalType == 'turnIn' ?
      [
        {
          label: '党员名称',
          keyName: 'partyMemberName',
          disabled: true,
          type: 'BaseInput',
          span: 12,
        },
        {
          label: '转入党支部',
          keyName: 'partyOrgId',
          type: PartyTreeSelecgt,
          span: 12,
          attrs: {
            onlySelectTypes: [ 3 ],
            queryAll: true
          }
        },
        {
          label: '备注说明',
          keyName: 'remarks',
          type: 'BaseTextarea',
          span: 24,
        },
      ] : [
        {
          label: '党员名称',
          keyName: 'partyMemberName',
          disabled: true,
          type: 'BaseInput',
          span: 12,
        },
        {
          label: '备注说明',
          keyName: 'remarks',
          type: 'BaseTextarea',
          span: 24,
        }
      ]
    },
  },
  watch: {
    initData: {
      immediate: true,
      deep: true,
      handler(val) {
        this.formInfo = this.modalType == 'turnIn' ? ({ ...val, partyOrgId: void 0 } || {}) : ({ ...val } || {})
      },
    },
  },
 async mounted(){
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('Party_members_converted_out')
  },
  methods: {
    handleSelectOrg(key, data) {
      const { id } = data
      this.formInfo = {
        ...this.formInfo,
        partyOrgId: id,
      }
      this.$nextTick(() => {
        this.$refs.ruleForm.validateField('partyOrgId')
      })
    },
    handleOk() {
      this.setProcessConfig = null;
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          this.$message.warning("请填写完所有必填项信息")
          return
        }

      this.submitApprovalConfirm(this.processConfig, async(flag)=>{
              if(!flag) {
                this.loading[auditStatus] = false
                return
              }
           const params = {
              ...this.formInfo,
            }
            delete params.partyMemberName
            const api = this.modalType == 'turnIn' ? inwardTransfer : transferOut;
            const res = await api(params)
            if (res && res.code == 0) {
              if(this.modalType == 'turnIn'){
                //提交时，返回id
                this.businessId = this.formInfo.id;
                this.setProcessConfig = this.processConfig;
              }else{
                this.handleCancel();
              }
              this.$message.success(`${this.modalTitle}成功`)
              this.$emit('ok')
            } else {
              this.$message.error(`${this.modalTitle}失败！`);
            }
        })
      })
    },
    //弹窗关闭
     closeSelectApprover(){
      this.handleCancel();
     }, 
    handleCancel() {
      this.$emit('update:visible')
    },
  },
}
</script>

<style>
</style>
